﻿using System.Text.Json.Serialization;
using Transponder_Core.DataTarget;

namespace Transponder_Core.DataSource
{
    /// <summary>
    /// 
    /// </summary>
    public class EventReport
    {

        public EventReport() { }

        public string imei { get; set; }   =string.Empty;
        public decimal VolA{ get; set; }
        public decimal VolB { get; set; }
        public decimal VolC { get; set; }
        public decimal CurA { get; set; }
        public decimal CurB { get; set; }
        public decimal CurC { get; set; }
        public decimal Ta{ get; set; }
        public decimal Tb{ get; set; }
        public decimal Tc { get; set; }
        public decimal Tn { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// 
        [JsonIgnore]
        public DateTime eventTime { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public static EventReport FromJson(string json)
        {
            try
            {
                var obj = System.Text.Json.JsonSerializer.Deserialize<EventReport>(json);
                return obj;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string ToJson()
        {
            try
            {
                var obj = System.Text.Json.JsonSerializer.Serialize<DataSource.EventReport>(this);
                return obj;
            }
            catch
            {
                return string.Empty;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public  Payload ToPayload()
        {
            var ts = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();

            var payload = new Payload();
            
            payload.ts = ts;

            var device = new Device();
             
            device.ts = ts;

            MeasurePoint uaMeasurePoint = new MeasurePoint();
            uaMeasurePoint.m = MeasurePointKeys.Ua.ToString();
            uaMeasurePoint.v = (decimal)this.VolA;
            uaMeasurePoint.ts = ts;
            device.d.Add(uaMeasurePoint);


            MeasurePoint ubMeasurePoint = new MeasurePoint();
            ubMeasurePoint.m = MeasurePointKeys.Ub.ToString();
            ubMeasurePoint.v = (decimal)this.VolB;
            ubMeasurePoint.ts = ts;
            device.d.Add(ubMeasurePoint);

            MeasurePoint ucMeasurePoint = new MeasurePoint();
            ucMeasurePoint.m = MeasurePointKeys.Ub.ToString();
            ucMeasurePoint.v = (decimal)this.VolC;
            ucMeasurePoint.ts = ts;
            device.d.Add(ucMeasurePoint);

            MeasurePoint iaMeasurePoint = new MeasurePoint();
            iaMeasurePoint.m = MeasurePointKeys.Ia.ToString();
            iaMeasurePoint.v = (decimal)this.CurA;
            iaMeasurePoint.ts = ts;
            device.d.Add(iaMeasurePoint);


            MeasurePoint ibMeasurePoint = new MeasurePoint();
            ibMeasurePoint.m = MeasurePointKeys.Ib.ToString();
            ibMeasurePoint.v = (decimal)this.CurB;
            ibMeasurePoint.ts = ts;
            device.d.Add(ibMeasurePoint);

            MeasurePoint icMeasurePoint = new MeasurePoint();
            icMeasurePoint.m = MeasurePointKeys.Ic.ToString();
            icMeasurePoint.v = (decimal)this.CurC;
            icMeasurePoint.ts = ts;
            device.d.Add(icMeasurePoint);
             

            MeasurePoint taMeasurePoint = new MeasurePoint();
            taMeasurePoint.m = MeasurePointKeys.Ta.ToString();
            taMeasurePoint.v = (decimal)this.Ta;
            taMeasurePoint.ts = ts;
            device.d.Add(taMeasurePoint);

            MeasurePoint tbMeasurePoint = new MeasurePoint();
            tbMeasurePoint.m = MeasurePointKeys.Tb.ToString();
            tbMeasurePoint.v = (decimal)this.Tb;
            tbMeasurePoint.ts = ts;
            device.d.Add(tbMeasurePoint);

            MeasurePoint tcMeasurePoint = new MeasurePoint();
            tcMeasurePoint.m = MeasurePointKeys.Tb.ToString();
            tcMeasurePoint.v = (decimal)this.Tc;
            tcMeasurePoint.ts = ts;
            device.d.Add(tcMeasurePoint);


            MeasurePoint tnMeasurePoint = new MeasurePoint();
            tnMeasurePoint.m = MeasurePointKeys.Tn.ToString();
            tnMeasurePoint.v = (decimal)this.Tc;
            tnMeasurePoint.ts = ts;
            device.d.Add(tnMeasurePoint);




            payload.devs.Add(device);
            return payload;
        }

    }
}
